﻿using System;
using System.Windows.Forms;
using iTunesLib;
using Onyeyiri;

namespace iTunes_Media_Keys
{
    public partial class MainForm : Form
    {
        iTunesApp iTunes = new iTunesApp();
        HotKeyManager hKM = new HotKeyManager();
        AddHotKey aHK;
        HotKeyIniManager hKIM;

        public MainForm()
        {
            InitializeComponent();
        }

        #region Form Events

        private void MainForm_Load(object sender, EventArgs e)
        {
            hKIM = new HotKeyIniManager(System.IO.Directory.GetCurrentDirectory());
            hKM.HotKeyHit += new HotKeyEventHandler(hKM_HotKeyHit);
            //
            HotKeyProfile[] profs = hKIM.LoadHotKeys();
            foreach (HotKeyProfile prof in profs)
            {
                addHotKey((Keys)prof.Key, prof.Modifier, prof.Function);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            aHK = new AddHotKey();
            aHK.HotKeyAdded += new AddHotKeyHandler(aHK_HotKeyAdded);
            aHK.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                hKM.Unregister(Convert.ToInt32(listView1.SelectedItems[0].SubItems[3].Text));
                listView1.SelectedItems[0].Remove();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //settings
        }

        #endregion

        #region Events

        private void hKM_HotKeyHit(int hotKeyId)
        {
            switch (hotKeyId)
            {
                case (int)iTunesFunctions.Play:
                    iTunes.Play();
                    break;
                case (int)iTunesFunctions.Pause:
                    iTunes.Pause();
                    break;
                case (int)iTunesFunctions.PlayPause:
                    iTunes.PlayPause();
                    break;
                case (int)iTunesFunctions.Stop:
                    iTunes.Stop();
                    break;
                case (int)iTunesFunctions.PreviousTrack:
                    iTunes.PreviousTrack();
                    break;
                case (int)iTunesFunctions.NextTrack:
                    iTunes.NextTrack();
                    break;
                case (int)iTunesFunctions.Rewind:
                    iTunes.Rewind();
                    break;
                case (int)iTunesFunctions.FastForward:
                    iTunes.FastForward();
                    break;
                case (int)iTunesFunctions.Rate0:
                    iTunes.CurrentTrack.Rating = 0;
                    break;
                case (int)iTunesFunctions.Rate1:
                    iTunes.CurrentTrack.Rating = 1;
                    break;
                case (int)iTunesFunctions.Rate2:
                    iTunes.CurrentTrack.Rating = 2;
                    break;
                case (int)iTunesFunctions.Rate3:
                    iTunes.CurrentTrack.Rating = 3;
                    break;
                case (int)iTunesFunctions.Rate4:
                    iTunes.CurrentTrack.Rating = 4;
                    break;
                case (int)iTunesFunctions.Rate5:
                    iTunes.CurrentTrack.Rating = 5;
                    break;
            }
        }

        private void aHK_HotKeyAdded(KeyEventArgs k, HotKeyModifier h, iTunesFunctions i)
        {
            addHotKey(k.KeyCode, h, i);
            hKIM.SaveHotKey(new HotKeyProfile(h, k.KeyValue, i));
        }

        #endregion

        #region Private Functions

        private void addHotKey(Keys k, HotKeyModifier h, iTunesFunctions i)
        {
            hKM.Register(k, h, (int)i, null);
            ListViewItem item = new ListViewItem(new[] { iTunesFunctionsToString(i), (h.ToString() + " + " + k.ToString()), ((int)i).ToString() });
            listView1.Items.Add(item);
        }

        private string iTunesFunctionsToString(iTunesFunctions i)
        {
            if (i == iTunesFunctions.Play) { return "Play"; }
            else if (i == iTunesFunctions.Pause) { return "Pause"; }
            else if (i == iTunesFunctions.PlayPause) { return "Play/Pause"; }
            else if (i == iTunesFunctions.Stop) { return "Stop"; }
            else if (i == iTunesFunctions.PreviousTrack) { return "Previous Track"; }
            else if (i == iTunesFunctions.NextTrack) { return "Next Track"; }
            else if (i == iTunesFunctions.Rewind) { return "Rewind"; }
            else if (i == iTunesFunctions.FastForward) { return "Fast Forward"; }
            else if (i == iTunesFunctions.Rate0) { return "Rate 0 Stars"; }
            else if (i == iTunesFunctions.Rate1) { return "Rate 1 Stars"; }
            else if (i == iTunesFunctions.Rate2) { return "Rate 2 Stars"; }
            else if (i == iTunesFunctions.Rate3) { return "Rate 3 Stars"; }
            else if (i == iTunesFunctions.Rate4) { return "Rate 4 Stars"; }
            else if (i == iTunesFunctions.Rate5) { return "Rate 5 Stars"; }
            else { return "error"; }
        }

        #endregion
    }

    #region iTunesFunctions

    public enum iTunesFunctions : int
    {
        Play          = 1,
        Pause         = 2,
        PlayPause     = 3,
        Stop          = 4,
        PreviousTrack = 5,
        NextTrack     = 6,
        Rewind        = 7,
        FastForward   = 8,
        Rate0         = 9,
        Rate1         = 10,
        Rate2         = 11,
        Rate3         = 12,
        Rate4         = 13,
        Rate5         = 14
    }

    #endregion
}
